//
//  MyMoneyViewController.m
//  HTP
//
//  Created by 王鑫 on 15/11/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "MyMoneyViewController.h"
#import "MyMoneyTableViewCell.h"
#import "MyBillsViewController.h"

#define headfootbutton ScreenWidth / 3.0
@interface MyMoneyViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) UITableView *myMoneyTable;

@property (nonatomic,retain) NSMutableArray *cellDate;

@property (nonatomic,retain) NSDictionary *recharge;

@property (nonatomic,retain) NSDictionary *getMoney;

@property (nonatomic,retain) NSDictionary *myBills;

@end

@implementation MyMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
        [self initNav];
}


//TODO:导航栏定义
- (void)initNav{
    
    [self addBackBtn:@""];
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
    
}

//TODO:UI绘制
- (void)initUI {
    
    [self setData];
    [self.view addSubview:[self getMyMoneyHeadView]];
    [self.view addSubview:self.myMoneyTable];
}

- (UIView *)getMyMoneyHeadView {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 170)];
    
    UIButton *personalInfoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 85)];
    [personalInfoButton setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"infor"]]];
    
    UIImageView *infoImage = [[UIImageView alloc] initWithFrame:CGRectMake((headfootbutton - 60) / 2.0, 5, 65, 65)];
    infoImage.image = [UIImage imageNamed:@"我的钱包_03"];
    [personalInfoButton addSubview:infoImage];
    
    UILabel *infoNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 10, 70, 25)];
    infoNameLabel.font = [UIFont systemFontOfSize:20.0f];
    infoNameLabel.text = @"陈嘉惠";
    infoNameLabel.textColor = [UIColor whiteColor];
    [personalInfoButton addSubview:infoNameLabel];
    
    UILabel *infoPhoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 40, 110, 25)];
    infoPhoneLabel.font = [UIFont systemFontOfSize:16.0f];
    infoPhoneLabel.text = @"18368493726";
    infoPhoneLabel.textColor = [UIColor whiteColor];
    [personalInfoButton addSubview:infoPhoneLabel];
    
    UIImageView *inforNextImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30, 24, 15, 26.5)];
    inforNextImage.image = [UIImage imageNamed:@"我的钱包_06"];
    [personalInfoButton addSubview:inforNextImage];
    
    [headView addSubview:personalInfoButton];
    
    UIButton *moneyButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 85, headfootbutton, 85)];
    UILabel *moneyNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake((headfootbutton - 60) / 2.0, 15, 60, 40)];
    moneyNumberLabel.textColor = [UIColor colorWithRed:235 / 255.0 green:161 / 255.0 blue:0 alpha:1];
    moneyNumberLabel.font = [UIFont systemFontOfSize:23.0f];
    moneyNumberLabel.text = @"0.00";
    [moneyButton addSubview:moneyNumberLabel];
    
    UILabel *moneydaLabel = [[UILabel alloc] initWithFrame:CGRectMake(moneyNumberLabel.frame.size.width + (headfootbutton - 60) / 2.0 - 12, 28, 20, 20)];
    moneydaLabel.textColor = [UIColor colorWithRed:235 / 255.0 green:161 / 255.0 blue:0 alpha:1];
    moneydaLabel.font = [UIFont systemFontOfSize:12.0f];
    moneydaLabel.text = @"元";
    [moneyButton addSubview:moneydaLabel];
    
    UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake((headfootbutton - 55) / 2.0, 55, 55, 20)];
    moneyLabel.font = [UIFont systemFontOfSize:13.0f];
    moneyLabel.text = @"我的余额";
    [moneyButton addSubview:moneyLabel];
    
    [headView addSubview:moneyButton];
    [headView addSubview:[self buttonLine:CGRectMake(headfootbutton - 0.5, 85, 1, 85)]];
    
    UIButton *couponsButton = [[UIButton alloc] initWithFrame:CGRectMake(headfootbutton, 85, headfootbutton, 85)];
    UILabel *couponsNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake((headfootbutton - 30) / 2.0, 15, 30, 40)];
    couponsNumberLabel.textColor = [UIColor redColor];
    couponsNumberLabel.font = [UIFont systemFontOfSize:23.0f];
    couponsNumberLabel.text = @"0";
    [couponsButton addSubview:couponsNumberLabel];
    
    UILabel *couponsdaLabel = [[UILabel alloc] initWithFrame:CGRectMake((headfootbutton - 30) / 2.0 + 15, 28, 20, 20)];
    couponsdaLabel.textColor = [UIColor redColor];
    couponsdaLabel.font = [UIFont systemFontOfSize:12.0f];
    couponsdaLabel.text = @"个";
    [couponsButton addSubview:couponsdaLabel];
    
    UILabel *couponsLabel = [[UILabel alloc] initWithFrame:CGRectMake((headfootbutton - 40) / 2.0, 55, 40, 20)];
    couponsLabel.font = [UIFont systemFontOfSize:13.0f];
    couponsLabel.text = @"优惠劵";
    [couponsButton addSubview:couponsLabel];
    
    [headView addSubview:couponsButton];
    [headView addSubview:[self buttonLine:CGRectMake(headfootbutton * 2 - 0.5, 85, 1, 85)]];
    
    UIButton *lessonButton = [[UIButton alloc] initWithFrame:CGRectMake(headfootbutton * 2, 85, headfootbutton, 85)];
    UILabel *lessonNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake((headfootbutton - 30) / 2.0, 15, 30, 40)];
    lessonNumberLabel.textColor = [UIColor colorWithRed:148 / 255.0 green:191 / 255.0 blue:109 / 255.0 alpha:1];
    lessonNumberLabel.font = [UIFont systemFontOfSize:23.0f];
    lessonNumberLabel.text = @"0";
    [lessonButton addSubview:lessonNumberLabel];
    
    UILabel *lessondaLabel = [[UILabel alloc] initWithFrame:CGRectMake((headfootbutton - 30) / 2.0 + 15, 28, 20, 20)];
    lessondaLabel.textColor = [UIColor colorWithRed:148 / 255.0 green:191 / 255.0 blue:109 / 255.0 alpha:1];
    lessondaLabel.font = [UIFont systemFontOfSize:12.0f];
    lessondaLabel.text = @"个";
    [lessonButton addSubview:lessondaLabel];
    
    UILabel *lessonLabel = [[UILabel alloc] initWithFrame:CGRectMake((headfootbutton - 30) / 2.0, 55, 30, 20)];
    lessonLabel.font = [UIFont systemFontOfSize:13.0f];
    lessonLabel.text = @"课程";
    [lessonButton addSubview:lessonLabel];
    
    [headView addSubview:lessonButton];
    
    headView.backgroundColor = [UIColor whiteColor];
    return headView;
}

- (UIImageView *)buttonLine:(CGRect)rect {
    UIImageView *line = [[UIImageView alloc] initWithFrame:rect];
    line.image = [UIImage imageNamed:@"line_qianbao"];
    return line;
}

- (UITableView *)myMoneyTable {
    if (!_myMoneyTable) {
        _myMoneyTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 170, ScreenWidth, ScreenHeihgt - 64) style:UITableViewStyleGrouped];
        [_myMoneyTable setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_qianbao"]]];
        _myMoneyTable.delegate = self;
        _myMoneyTable.dataSource = self;
        _myMoneyTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _myMoneyTable;
}

//TODO:选项信息设置
- (void)setData {
    _recharge = @{
                      @"icoName":@"renminbi",
                      @"infoText":@"充值",
                      };
    _getMoney = @{
                      @"icoName":@"meiyuan",
                      @"infoText":@"提现",
                      };
    _myBills = @{
                      @"icoName":@"zhangdan",
                      @"infoText":@"我的账单",
                      };
    _cellDate = [NSMutableArray arrayWithObjects:_recharge, _getMoney, _myBills, nil];
}

//TODO:tableview的代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }else
        return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *Identifier = @"Identifier";
    MyMoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    
    if (cell == nil) {
        cell = [[MyMoneyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    
    NSInteger num = indexPath.section * 2 + indexPath.row;
    [cell layoutSubviews:indexPath.section];
    [cell setCellData:[_cellDate objectAtIndex:num]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 20;
    }else
        return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        MyBillsViewController *myBillsView = [[MyBillsViewController alloc] init];
        [myBillsView addCustomTitleWithTitle:@"我的账单"];
        [[NSNotificationCenter defaultCenter] postNotificationName:ROOTGOTONEXTCONTROLLER object:myBillsView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
